package com.ace.config.datasource;

import com.ace.core.utils.ResKit;
import com.baomidou.mybatisplus.entity.GlobalConfiguration;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * 数据源配置
 *
 *
 * Created by com.ace on 2018/3/9.
 */
@EnableTransactionManagement
@PropertySource("classpath:jdbc.properties")
@Configuration
/*添加jdbc配置文件*/
public class DataSourceConfig implements EnvironmentAware {
    private Environment em;

    @Bean
    public MapperScannerConfigurer mappers(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        /*下面是所有的Dao*/
        mapperScannerConfigurer.setBasePackage("com.ace.persistence.dao");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        System.out.println("注入MapperScanner");
        return mapperScannerConfigurer;
    }

    public void setEnvironment(Environment environment) {
        this.em = environment;
    }

    /**
     * spring与MyBatis整合
     */
    @Bean
    public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSource dataSource, GlobalConfiguration globalConfiguration){
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));;
        /*批量获取资源文件*/
        System.out.println(Thread.currentThread().getContextClassLoader()
                .getResource(""));
        Resource[] classPathResources = ResKit.getClassPathResources("classpath*:com/ace/persistence/dao/mapping/*.xml");
        sqlSessionFactoryBean.setMapperLocations(classPathResources);

        /*对mybatis-plus进行配置*/
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType("mysql");
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{paginationInterceptor});
        sqlSessionFactoryBean.setGlobalConfig(globalConfiguration);
        System.out.println("mybatis-plus完毕");
        return sqlSessionFactoryBean;

    }

    @Bean
    public DriverManagerDataSource dataSource(){
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(em.getProperty("jdbc.driverClassName"));
        driverManagerDataSource.setUrl(em.getProperty("jdbc.url"));
        driverManagerDataSource.setUsername(em.getProperty("jdbc.username"));
        driverManagerDataSource.setPassword(em.getProperty("jdbc.password"));

        DataSourceConfigTemplate.config(driverManagerDataSource);
        return driverManagerDataSource;
    }

    /**
     * 事务管理, 声明式事务的开启
     *
     * @author ace
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager manager = new DataSourceTransactionManager();
        manager.setDataSource(dataSource);
        return manager;
    }

    /**
     *
     */
    @Bean
    public GlobalConfiguration globalConfiguration(){
        GlobalConfiguration globalConfiguration = new GlobalConfiguration();

        globalConfiguration.setIdType(1);
        globalConfiguration.setDbType("mysql");
        System.out.println("MyBatis-Plu获取全局设置");

        /**
         * 全局表名为下划线命名设置
         */
        globalConfiguration.setDbColumnUnderline(false);

        return globalConfiguration;
    }
}

